package com.uphone.kingmall.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.view.dialog.OnDialogViewClickListener;
import com.uphone.kingmall.view.dialog.RadishDialog;

import java.io.File;

/**
 * Created by Burt on 2017/11/23.
 * E-mail:burtmail@163.com
 * Blog: https://burthub.github.io/Blog/
 * Copyright infringement!
 */

public class MapSoftUtil {
    public static void dialogChoiceMap(final Activity context, final String lat, final String lon, final String key) {

        new RadishDialog.Builder(context)
                .setView(R.layout.kingmall_dialog_map)
                .setFromBottom(true)
                .setClick(R.id.tv_gaode, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(RadishDialog dialog, View view) {
                        MapSoftUtil.goToNaviActivity(context, key, lat + "", lon + "");
                        dialog.dismiss();
                    }
                })
                .setClick(R.id.dialog_cancel, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(RadishDialog dialog, View view) {
                        dialog.dismiss();
                    }
                })
                .show();
    }


    /**
     * 启动高德App进行导航
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/6/27,13:58
     * <h3>UpdateTime</h3> 2016/6/27,13:58
     * <h3>CreateAuthor</h3>
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     *
     * @param poiname           非必填 POI 名称
     * @param lat               必填 纬度
     * @param lon               必填 经度
     */
    public static void goToNaviActivity(Context context, String poiname, String lat, String lon) {
        boolean appInstalled = isAppInstalled(context, "com.autonavi.minimap");
        LogUtils.e("appInstalled1:" + appInstalled);
        if (!appInstalled) {
            AppUtil.showBrowserUpdateDialog(context, "高德地图", "market://details?id=" + "com.autonavi.minimap");
        } else {
            try {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setPackage("com.autonavi.minimap");
                intent.setData(Uri.parse("amapuri://poi/detail?poiname=" + poiname + "&lat=" + lat + "&lon=" + lon));
                context.startActivity(intent);
            } catch (Exception e) {
                LogUtils.e("高德地图异常：" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据包名检测某个APP是否安装
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2016/6/27,13:02
     * <h3>UpdateTime</h3> 2016/6/27,13:02
     * <h3>CreateAuthor</h3>
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     *
     * @param packageName 包名
     * @return true 安装 false 没有安装
     */
    public static boolean isInstallByRead(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

    /**
     * 根据包名检测某个APP是否安装
     */
    private static boolean isAppInstalled(Context context, String uri) {
        PackageManager pm = context.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }
}
